let express = require('express');
let router = express.Router();

const circleService = require('../service/circleService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

/*
 * 懒加载获取圈子接口
 */
router.post('/getCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { lastId } = req.body;
    let result = await circleService.getCircle(uid, lastId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取圈子详细信息
 */
router.post('/getCircleInfo', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    let result = await circleService.getCircleInfo(uid, circleId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 发布评论
 */
router.post('/comment', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId, comment } = req.body;
    // 删除comment中所有的空白字符
    let format_comment = comment.replace(/\s+/g, '');
    if (!format_comment) {
        return res.status(400).json({ success: false, msg: "评论不能为空" });
    }
    let result = await circleService.comment(uid, circleId, comment);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取所有评论信息
 */
router.post('/getComment', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    let result = await circleService.getComment(circleId, uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 点赞圈子
 */
router.post('/likeCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    let result = await circleService.likeCircle(uid, circleId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 取消点赞圈子
 */
router.post('/nolikeCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    let result = await circleService.nolikeCircle(uid, circleId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 分页获取所有圈子的接口
 */
router.get('/getAllCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { page } = req.query;
    let result = await circleService.getAllCircle(uid, page);
    res.status(result.success ? 200 : 400).json(result);
});

/**
 * 发布圈子
 */
router.post('/addCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleTitle, circleContent, selectedImage } = req.body;
    let format_circleTitle = circleTitle.replace(/\s+/g, '');
    let format_circleContent = circleContent.replace(/\s+/g, '');
    if (!format_circleTitle || !format_circleContent || !selectedImage) {
        return res.status(400).json({ success: false, msg: "参数错误" });
    }
    let result = await circleService.addCircle(uid, circleTitle, circleContent, selectedImage);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除圈子
 */
router.post('/deleteCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    if (!circleId) {
        return res.status(400).json({ success: false, msg: "参数错误" });
    }
    let result = await circleService.deleteCircle(uid, circleId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 管理员删除帖子
 */
router.post('/adminDeleteCircle', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let { circleId } = req.body;
    if (!circleId) {
        return res.status(400).json({ success: false, msg: "参数错误" });
    }
    let result = await circleService.adminDeleteCircle(uid, circleId);
    res.status(result.success ? 200 : 400).json(result)
})

module.exports = router;
